package com.sun.module_mine.ui

import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.sharp.KeyboardArrowRight
import androidx.compose.material.icons.filled.AccessTime
import androidx.compose.material.icons.filled.AccountCircle
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import com.guru.fontawesomecomposelib.FaIcon
import com.guru.fontawesomecomposelib.FaIcons
import com.sun.module_base.theme.white
import com.sun.module_common.event.getEventViewModel
import com.sun.module_common.route.RouteName
import com.sun.module_common.route.RouteUtils
import com.sun.module_common.sp.SpUtils
import com.sun.module_mine.ui.timer.TimerAnimation
import com.sun.module_mine.ui.timer.TimerData
import com.sun.module_mine.ui.timer.TimerViewAnimation
import com.sun.module_network.utils.logi


/**
 *@author:sunc
 *@time :2025/7/29 16:57
 *@desc: 描述
 */

@Preview(showBackground = true)
@Composable
fun MineScreen(
    modifier: Modifier = Modifier, navCtrl: NavHostController? = null,
    viewModel: MineViewModel = hiltViewModel()
) {
    "MineScreen".logi()

//    val isLogin = remember { mutableStateOf(SpUtils.isLogin) }
    val isLogin = remember (SpUtils.isLogin){ SpUtils.isLogin }

    val userName = remember { mutableStateOf("未登录") }
    LaunchedEffect(getEventViewModel().userInfo) {
        getEventViewModel().userInfo.collect {
            userName.value = it?.nickname?:""
        }
    }

    val timerData: TimerData by viewModel.timeState.collectAsState()
    Column(
        modifier = modifier
//            .safeContentPadding()
//            .statusBarsPadding()
            .fillMaxSize()
//            .background(themeColor)
//            .wrapContentSize(Alignment.Center)
    ) {

        Box(
            modifier = Modifier
                .fillMaxWidth()
                .height(200.dp)
                .background(color = MaterialTheme.colorScheme.primary)
                .padding(bottom = 20.dp)
        ) {
            Column(
                modifier = Modifier.align(Alignment.Center),
                horizontalAlignment = Alignment.CenterHorizontally
            ) {
                FaIcon(
                    faIcon = FaIcons.UserCircle,
                    size = 100.dp,
                    tint = MaterialTheme.colorScheme.background
                )
                TextButton(onClick = {
                    if (!isLogin) {
                        RouteUtils.navigateTo(RouteName.Login)
                    }
                }) {
                    Text(
                        text = userName.value, fontSize = 13.sp,
                        color = MaterialTheme.colorScheme.background
                    )
                }
            }
//            Icon(
//                imageVector = Icons.Default.AccountCircle,
//                modifier = Modifier
//                    .size(100.dp)
//                    .align(Alignment.Center)
//                    .clickable {
//                        RouteUtils.navigateTo(RouteName.Login)
//                    },
//                contentDescription = "用户头像",
//                tint = white
//            )
            TimerViewAnimation(
                timerData = timerData,
                modifier = Modifier.align(Alignment.BottomCenter),
                timeAnimation = TimerAnimation.SLID_BOTTOM,
                textColor = MaterialTheme.colorScheme.onSurface,
                icon = Icons.Default.AccessTime,
                iconColor = ColorFilter.tint(MaterialTheme.colorScheme.onSurface)
            )
        }
        Column(
            modifier = Modifier
                .fillMaxWidth()
                .padding(horizontal = 16.dp)
        ) {
            listOf("我的收藏", "检查更新", "密码锁屏", "关于我们").forEach { string ->
                Spacer(Modifier.height(10.dp))
                MineItem(str = string) {
                    when (string) {
                        "我的收藏" -> {}
                        "检查更新" -> {}
                        "密码锁屏" -> {
                            RouteUtils.navigateTo(RouteName.PinLock)
                        }

                        "关于我们" -> {}
                    }
                }
            }
        }
    }
}

@Composable
fun MineItem(modifier: Modifier = Modifier, str: String, onClick: () -> Unit = {}) {

    Row(
        modifier = modifier
            .fillMaxWidth()
            .height(38.dp)
            .border(
                width = 0.5.dp,
                color = MaterialTheme.colorScheme.onSurface.copy(alpha = 0.3f),
                shape = RoundedCornerShape(50)
            )
            .clip(RoundedCornerShape(50))
            .clickable { onClick.invoke() }
            .padding(horizontal = 16.dp),
        horizontalArrangement = Arrangement.SpaceBetween,
        verticalAlignment = Alignment.CenterVertically
    ) {
        Text(str, fontSize = 14.sp)
        Icon(
            imageVector = Icons.AutoMirrored.Sharp.KeyboardArrowRight,
            contentDescription = null,
            modifier = Modifier.size(20.dp)
        )
    }
}
